import ServerConfig from "../server.json";
import { ResultHandler } from './middleware';
const USER_ID_PROPERTY_NAME = "_covidUserId";
const LIST_ID_PROPERTY_NAME = "_covidListId";
const RELATION_ID_PROPERTY_NAME = "_covidRelationId";

const baseUrl = ServerConfig.baseUrl;
const apis = ServerConfig.apis;

const useAPIs = {
	async userBooking(data) {
	    const result = await uni.request({
	        url: baseUrl + apis.userBooking.url,
	        method: apis.userBooking.method,
	        data: data
	    });
	    return ResultHandler(result);
	},
	async getUserBooking(id) {
	    const result = await uni.request({
	        url: baseUrl + apis.getUserBooking.url + "/" + id,
	        method: apis.getUserBooking.method
	    });
	    return ResultHandler(result);
	},
	async confirm(data) {
	    const result = await uni.request({
	        url: baseUrl + apis.userScan.url,
	        method: apis.userScan.method,
	        data: data
	    });
	    return ResultHandler(result);
	},
	async cancel(id) {
	    const result = await uni.request({
	        url: baseUrl + apis.userCancel.url + "/" + id,
	        method: apis.userCancel.method
	    });
	    return ResultHandler(result);
	},
    async scan(stationId, userId) {
		const result = await uni.request({
	        url: baseUrl + apis.userScan.url + "/" + stationId + "/" + userId,
	        method: apis.userScan.method
	    });
	    return ResultHandler(result);
	},
    getUserIdentifier() {
        return localStorage.getItem(USER_ID_PROPERTY_NAME);
    },
    setUserIdentifier(id) {
        localStorage.setItem(USER_ID_PROPERTY_NAME, id);
    },
    clearUserIdentifier() {
        localStorage.removeItem(USER_ID_PROPERTY_NAME);
    },
    getListIdentifier() {
        return localStorage.getItem(LIST_ID_PROPERTY_NAME);
    },
    setListIdentifier(id) {
        localStorage.setItem(LIST_ID_PROPERTY_NAME, id);
    },
    clearListIdentifier() {
        localStorage.removeItem(LIST_ID_PROPERTY_NAME);
    },
    getRelationIdentifier() {
        return localStorage.getItem(RELATION_ID_PROPERTY_NAME) && JSON.parse(localStorage.getItem(RELATION_ID_PROPERTY_NAME));
    },
    setRelationIdentifier(listId, userId) {
        localStorage.setItem(RELATION_ID_PROPERTY_NAME, JSON.stringify({ [listId]: userId }));
    },
    clearRelationIdentifier() {
        localStorage.removeItem(RELATION_ID_PROPERTY_NAME);
    }
};

export default useAPIs;